<template>
  <div class="approval">
    <el-card>
      <div class="title">{{$t('callRecord.RecentData')}}</div>
      <div class="recent-data">
        <!-- 今日未拨打订单 -->
        <div class="recent-item" @click="recentFn(3)">
          <div>
            <b>{{recentDataObj.unDialCount}}</b>
          </div>
          <div>{{$t('electricRecord.todayNotDialed')}}</div>
        </div>
        <!-- 今日未接通订单 -->
        <div class="recent-item" @click="recentFn(4)">
          <div>
            <b>{{recentDataObj.unConnectedCount}}</b>
          </div>
          <div>{{$t('callRecord.todayUnconnected')}}</div>
        </div>
      </div>
    </el-card>
    <el-card style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('approval.title2')}}</div>
      <el-table
        :row-class-name="unconnectClass"
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        size="mini"
      >
        <el-table-column header-align="center" align="center" width="65" :label="$t('user.num')">
          <template slot-scope="scope">
            <span>{{scope.row.index}}</span>
          </template>
        </el-table-column>
        <!-- 客户姓名 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="userRealName"
          :label="$t('approval.realName')"
          width="140"
        ></el-table-column>
        <!-- 手机号 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="userPhone"
          :label="$t('approval.phoneNo')"
          width="140"
        ></el-table-column>
        <!-- 上一笔结清业务ID -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lastFiveOrderId"
          :label="$t('electricRecord.LastSettleOrderId')"
          width="140"
        ></el-table-column>
        <!-- 结清本金 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lentAmt"
          :label="$t('electricRecord.settlePrincipal')"
          width="140"
        ></el-table-column>
        <!-- 借款期限 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="pTerm"
          :label="$t('product.LoanPeriod')"
          width="140"
        ></el-table-column>
        <!-- 业务渠道 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="channelName"
          :label="$t('approval.channel')"
          width="140"
        ></el-table-column>
        <!-- 借款App -->
        <el-table-column
          header-align="center"
          align="center"
          prop="appPackageName"
          :label="$t('approvalDetail.loanApp')"
          width="140"
        ></el-table-column>
        <!-- 用户状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="saleStatus"
          :label="$t('electricRecord.UserStatus')"
          width="140"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.saleStatus === 1">{{$t('electricRecord.RegistNonBillLading')}}</span>
            <span v-else-if="scope.row.saleStatus === 2">{{$t('electricRecord.AuthNonBillLading')}}</span>
            <span v-else-if="scope.row.saleStatus === 3">{{$t('electricRecord.SettleUnrenewedLoan')}}</span>
            <span v-else-if="scope.row.saleStatus === 4">{{$t('electricRecord.unregistered')}}</span>
          </template>
        </el-table-column>
        <!-- 分配时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="distributionTime"
          :label="$t('callRecord.AllocationTime')"
          width="140"
        ></el-table-column>
        <!-- 接通记录 -->
        <el-table-column
          header-align="center"
          align="center"
          :label="$t('electricRecord.ConnectRecord')"
          width="140"
        >
          <template slot-scope="scope">
            <!-- 查看记录 -->
            <el-button
              size="small"
              type="text"
              @click="recordFn(scope.row)"
            >{{$t('electricRecord.ViewRecord')}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('user.operation')" width="180" fixed="right">
          <template slot-scope="scope">
            <!-- 接通 -->
            <el-button
              size="small"
              type="text"
              @click="connectFn(scope.row)"
            >{{$t('callRecord.Connect')}}</el-button>
            <!-- 未接通 -->
            <el-button
              size="small"
              type="text"
              @click="unconnectedFn(scope.row)"
              style="color: #67c23a;"
            >{{$t('callRecord.Unconnected')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 电话记录 -->
    <el-dialog
      :title="$t('electricRecord.tellRecord')"
      :visible.sync="dialogConnectVisible"
      width="40%"
    >
      <el-form
        :model="connectform"
        :rules="connectRules"
        ref="connectform"
        label-width="100px"
        label-position="left"
      >
        <!-- 意向等级 -->
        <el-form-item :label="$t('electricRecord.IntentionLevel')" prop="desireLevelId">
          <el-select v-model="connectform.desireLevelId" :placeholder="$t('AppVersion.ruleCodeType')">
            <el-option v-for="item in levelArr" 
              :key="item.id"
              :label="item.dictValue"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <!-- 备注 -->
        <el-form-item :label="$t('user.remarks')" prop="checkRemark">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 5}"
            v-model="connectform.checkRemark"
            :placeholder="$t('electricRecord.DescAfterCall')"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" style="text-align: center;">
        <el-button @click="connectCancel('connectform')">{{$t('btn.cancel')}}</el-button>
        <el-button type="primary" @click="connectSubmit('connectform')">{{$t('btn.confirm')}}</el-button>
      </div>
    </el-dialog>
    <!-- 拨打记录 -->
    <el-dialog :title="$t('callRecord.DialRecord')" :visible.sync="dialogRecordVisible" width="40%">
      <div style="padding-bottom: 10px;box-sizing: border-box;">
        <el-table :data="recordData" border stripe style="width: 100%">
          <!-- 时间 -->
          <el-table-column
            header-align="center"
            align="center"
            :label="$t('approvalDetail.riskTime')"
            prop="dealTime"
            width="140"
          ></el-table-column>
          <!-- 电销员 -->
          <el-table-column
            header-align="center"
            align="center"
            :label="$t('electricRecord.ElecSalesman')"
            prop="checkUserName"
            width="140"
          ></el-table-column>
          <!-- 意向等级 -->
          <el-table-column
            header-align="center"
            align="center"
            :label="$t('electricRecord.IntentionLevel')"
            prop="desireLevelValue"
            width="160"
          ></el-table-column>
          <!-- 备注 -->
          <el-table-column
            header-align="center"
            align="center"
            :label="$t('user.remarks')"
            prop="checkRemark"
            width="200"
          ></el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { formatAmt } from "utils/index";
import {
  recentData,
  dialPage,
  modifyElectricPinStatus,
  callHistoryRecords,
  getDict
} from "api/electricRecord/index";
export default {
  data() {
    return {
      recentDataObj: {
        unDialCount: 0,
        unConnectedCount: 0
      },
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        pageType: 1
      },
      tableData: [],
      dialogConnectVisible: false,
      connectform: {
        id: 123,
        desireLevelId: "",
        desireLevelValue: "1212",
        checkRemark: ""  
      },
      connectRules: {
        desireLevelId: [
          {
            required: true,
            message: this.$t("electricRecord.RuleCustLevel"),
            trigger: "blur"
          }
        ],
        checkRemark: [
          {
            required: true,
            message: this.$t("RiskManager.ruleDesc"),
            trigger: "blur"
          }
        ]
      },
      levelArr: [],
      dialogRecordVisible: false,
      connectRow: "",
      recordData: []
    };
  },
  created() {
    if (this.ElecNotDialedPage.has(this.$route.path)) {
      this.listQuery = this.ElecNotDialedPage.get(this.$route.path);
    }
    this.getList();
    // 近期数据回显
    let obj = {};
    obj.type = 1;
    recentData(obj).then(res => {
      if (res.status === 200) {
        this.recentDataObj = res.data;
      } else {
        this.$message({
          showClose: true,
          type: "warning",
          message: res.message
        });
      }
    });
  },
  computed: {
    ...mapGetters(["elements", "ElecNotDialedPage"])
  },
  methods: {
    // 点击近期数据
    recentFn(type) {
      this.listQuery.page = 1;
      this.listQuery.pageType = type;
      this.getList();
    },
    // 接通
    connectFn(row) {
      // 获取意向等级
      getDict().then(res => {
        if (res.status === 200) {
          this.levelArr = res.data
        }
      })
      this.connectform.userTelStatus = 3;
      this.connectform.desireLevelId = "";
      this.connectform.checkRemark = "";
      this.connectRow = row;
      this.dialogConnectVisible = true;
    },
    // 接通取消
    connectCancel(formName) {
      this.dialogConnectVisible = false;
      this.$refs[formName].resetFields();
    },
    // 接通确认
    connectSubmit(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning",
            center: true
          })
            .then(() => {
              this.levelArr.forEach(item => {
                if (this.connectform.desireLevelId === item.id) {
                  this.connectform.desireLevelValue = item.dictValue;
                }
              })
              this.connectform.id = this.connectRow.id;
              modifyElectricPinStatus(this.connectform).then(res => {
                if (res.status === 200) {
                  this.dialogConnectVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t("user.success"),
                    message: this.$t("user.crtsuccess"),
                    type: "success",
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: res.message
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                showClose: true,
                type: "info",
                message: this.$t("loanDetail.cue3")
              });
            });
        } else {
          return false;
        }
      });
    },
    // 未接通
    unconnectedFn(row) {
      let obj = {};
      obj.id = row.id;
      obj.userTelStatus = 2;
      modifyElectricPinStatus(obj).then(res => {
        if (res.status === 200) {
          this.getList();
          this.$notify({
            title: this.$t("user.success"),
            message: this.$t("user.crtsuccess"),
            type: "success",
            duration: 2000
          });
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
        }
      });
    },
    // 查看记录
    recordFn(row) {
      let obj = {};
      obj.id = row.id;
      callHistoryRecords(obj).then(res => {
        if (res.status === 200) {
          this.recordData = res.data;
          this.dialogRecordVisible = true;
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
        }
      })
    },
    // 标记颜色
    unconnectClass(row) {
      if (row.row.userTelStatus === 2) {
        return 'allocated-row';
      } else {
        return '';
      }
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveElecNotDialedPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      dialPage(this.listQuery).then(res => {
        if (res.status === 200) {
          res.data.rows.forEach(item => {
            if (item.lentAmt) {
              item.lentAmt = formatAmt(item.lentAmt);
            }
          });
          this.listLoading = false;
          this.tableData = res.data.rows;
          this.tableData.forEach((item, index) => {
            item.index =
              index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
          });
          this.total = Number(res.data.total);
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
        }
      });
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
.recent-data {
  display: flex;
  flex-wrap: wrap;
  font-size: 14px;
  margin-bottom: 10px;
  .recent-item {
    height: 70px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border: 1px solid #aaa;
    text-align: center;
    border-radius: 10px;
    margin-top: 10px;
    margin-right: 40px;
    padding: 10px 20px;
    box-sizing: border-box;
    cursor: pointer;
    & > div:nth-child(1) {
      line-height: 24px;
      b {
        font-size: 18px;
      }
    }
  }
}
</style>